<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 订单
 *
 * admin-Orders
 *
 * @Apidoc\Title("订单")
 * @Apidoc\Group("api")
 */
class Orders extends BaseController
{
    /**
     * 新增
     *
     * admin-Orders-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Orders::create($data['orders']);
            return success();
        } else {
            $options = [];
            $orders__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['orders__member_id_options'] = $orders__member_id_option;
            $orders__point_products_id_option = \app\common\model\PointProducts::column('name as name, id as id');
            $options['orders__point_products_id_options'] = $orders__point_products_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-Orders-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Orders::update($data['orders']);
            return success();
        } else {
            $data = \app\common\model\Orders::find(input('orders__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['orders__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $orders__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['orders__member_id_options'] = $orders__member_id_option;
            $orders__point_products_id_option = \app\common\model\PointProducts::column('name as name, id as id');
            $options['orders__point_products_id_options'] = $orders__point_products_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-Orders-delete
     */
    public function delete()
    {
        \app\common\model\Orders::destroy(input('orders__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-Orders-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_orders');
        $query->join('db_member', 'db_member.id = db_orders.member_id');
        $query->join('db_point_products', 'db_point_products.id = db_orders.point_products_id');
        if (input('orders__name', '') !== '') {
            $query->where('db_orders.name', 'like', '%' . input('orders__name', '', 'search_in') . '%');
        }
        if (input('member__id', '') !== '') {
            $query->where('db_member.id', '=', input('member__id', '', 'search_in'));
        }
        if (input('point_products__id', '') !== '') {
            $query->where('db_point_products.id', '=', input('point_products__id', '', 'search_in'));
        }
        $query->where('db_orders.delete_time', '=', null);
        $query->where('db_member.delete_time', '=', null);
        $query->where('db_point_products.delete_time', '=', null);
        $query->field(['db_orders.id' => 'orders__id']);
        $query->field(['db_orders.details' => 'orders__details']);
        $query->field(['db_orders.trade_no' => 'orders__trade_no']);
        $query->field(['db_orders.create_time' => 'orders__create_time']);
        $query->field(['db_orders.total_amount' => 'orders__total_amount']);
        $query->field(['db_orders.payment_method' => 'orders__payment_method']);
        $query->field(['db_orders.integral' => 'orders__integral']);
        $query->field(['db_member.name' => 'member__name']);
        $query->field(['db_member.id' => 'member__id']);
        $query->field(['db_point_products.name' => 'point_products__name']);
        $query->order('db_orders.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        $point_products__id_option = \app\common\model\PointProducts::column('name as name, id as id');
        $options['point_products__id_options'] = $point_products__id_option;
        View::assign($options);
        return View::fetch();
    }
}